package com.guc.md.behavior

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.guc.kframe.utils.LogG

/**
 * Created by guc on 2021/5/27.
 * Description： 实现ToolBar和TextView联动的Behavior
 */
class ToolBarBehavior(context: Context, attrs: AttributeSet) :
    CoordinatorLayout.Behavior<TextView>(context, attrs) {
    private var mStartY = 0

    override fun layoutDependsOn(
        parent: CoordinatorLayout,
        child: TextView,
        dependency: View
    ): Boolean {
        return dependency is Toolbar
    }

    override fun onDependentViewChanged(
        parent: CoordinatorLayout,
        child: TextView,
        dependency: View
    ): Boolean {
        if (mStartY == 0) mStartY = dependency.y.toInt()
        val percent = dependency.y / mStartY //计算开始移动到结束的百分比（ToolBar 的高度/总高度）
        child.y = child.height * -percent
        LogG.loge("ToolBarBehavior", "mStartY:$mStartY  percent:$percent childY:${child.y}")
        return true
    }

}